//
//  TabBarVC.m
//  HappyRent
//
//  Created by CoderYee on 2017/3/28.
//  Copyright © 2017年 内圈科技. All rights reserved.
//

#import "TabBarVC.h"
#import "MineVC.h"
#import "MenuView.h"
#import "CollectionVC.h"

@interface TabBarVC ()<UITabBarControllerDelegate,UIGestureRecognizerDelegate>
{
    NSInteger     _VcSelectedIndex;//选择的下标
    MineVC         *_mineVC;
    MenuView       *_menuView;
    
    TabBarVCType  _VcType;
}
@end

@implementation TabBarVC
-(instancetype)init{
    
    if (self=[super init]) {
        
    }
    
    return self;
}
-(instancetype)initWithType:(TabBarVCType)VCtype{
    
    if (self=[super init]) {
        
        _VcType=VCtype;
        [self configureTab];
        [self addOwnVC];
        [self addRightVC];
    }
    
    return self;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
//    LodingView *lodingView = [LodingView loadingViewToView:self.view];
//    [lodingView startAnimation];
//    [self.view addSubview:lodingView];
//    
//    //[[LodingView  loadingViewToView:self.view] startAnimation];

}
#pragma mark --配置相关字体等
-(void)configureTab
{
    UITabBarItem *item                 = [UITabBarItem appearance];
    //设置默认状态文字的颜色
    NSMutableDictionary *md            = [NSMutableDictionary dictionary];
    
    md[NSForegroundColorAttributeName] = [UIColor  grayColor];
    [item setTitleTextAttributes:md forState:UIControlStateNormal];
    //设置高亮状态文字的颜色
    NSMutableDictionary *higMd            = [NSMutableDictionary dictionary];
    higMd[NSForegroundColorAttributeName] =kNavBarThemeColor;
    [item setTitleTextAttributes:higMd forState:UIControlStateSelected];
    self.tabBar.tintColor = kNavBarThemeColor;
    
}
-(void)addOwnVC{
    
    if (_VcType==BuyerType) {

        [self addOwnTabBarBuyerVC];
    }else{
        [self addOwnTabBarSellerVC];
    }
}

-(void)addOwnTabBarBuyerVC{
    

    self.tabBar.translucent = YES;
    //设置数据源
    NSArray *vcNames = @[@"HomePageVC",@"CollectionVC", @"BaseViewController",@"MineVC"];
    
    NSArray *imageArray=@[@"icon_shouye_weidianji",@"icon_shoucang_weidianji",@"tabbar_mall",@"icon_shezhi_weidianji"];
    
    NSArray *selectedimageArray=@[@"icon_shouye_yidianji",@"icon_shoucang_yidianji",@"tabbar_mallHLf",@"icon_shezhi_yidianji"];
    
    NSArray *titleArray=@[@"首页",@"收藏",@"消息",@"我的"];
    NSMutableArray *vcArr = [NSMutableArray array];
    NSMutableArray *nvArr = [NSMutableArray array];
    for (NSInteger i = 0; i < vcNames.count; i++)
    {
        Class vcClass = NSClassFromString(vcNames[i]);
        UIViewController *vc = [[vcClass alloc] init];
        vc.title = titleArray[i];
        vc.tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageArray[i]] ];
        vc.tabBarItem.selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@",selectedimageArray[i]]];
        UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
        [vcArr addObject:vc];
        [nvArr addObject:nc];
    }
    self.viewControllers = nvArr;
    self.delegate=self;
}

-(void)addOwnTabBarSellerVC{
    
    self.tabBar.translucent = YES;
    //设置数据源
    NSArray *vcNames = @[@"Seller_StockVC",@"Selller_OrderVC", @"BaseViewController",@"MineVC"];
    
    NSArray *imageArray=@[@"icon_shouye_weidianji",@"icon_shoucang_weidianji",@"tabbar_mall",@"icon_shezhi_weidianji"];
    
    NSArray *selectedimageArray=@[@"icon_shouye_yidianji",@"icon_shoucang_yidianji",@"tabbar_mallHLf",@"icon_shezhi_yidianji"];
    
    NSArray *titleArray=@[@"库存",@"订单",@"消息",@"我的"];
    NSMutableArray *vcArr = [NSMutableArray array];
    NSMutableArray *nvArr = [NSMutableArray array];
    for (NSInteger i = 0; i < vcNames.count; i++)
    {
        Class vcClass = NSClassFromString(vcNames[i]);
        UIViewController *vc = [[vcClass alloc] init];
        vc.title = titleArray[i];
        vc.tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageArray[i]] ];
        vc.tabBarItem.selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@",selectedimageArray[i]]];
        UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
        [vcArr addObject:vc];
        [nvArr addObject:nc];
    }
    self.viewControllers = nvArr;
    self.delegate=self;
}
-(void)addRightVC{
    
    _mineVC=[[MineVC alloc] init];
    [_mineVC.view setFrame:CGRectMake(0, 0, kMainScreenWidth*0.7, kMainScreenHeight)];
    _menuView =[[MenuView alloc] initWithDependencyView:self.view MenuView:_mineVC.view];
    [self addChildViewController:_mineVC];
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
   
    UINavigationController *temp = (UINavigationController *)viewController;
    if ([[temp.viewControllers objectAtIndex:0] isKindOfClass:[MineVC class]]) {
        [self showRightVC];
        return NO;
    }
    return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
    
}
-(void)showRightVC{
 
    [_menuView  show];
}
-(void)hideRightVCcompletion:(void (^ __nullable)(BOOL finished))completion{
 
    [_menuView  hidenWithoutAnimation];
    if (completion) {
        
        completion(YES);
    }
    
}
- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}
@end
